package com.eci.common.util;

import java.lang.reflect.Array;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;

import com.eci.common.asset.constant.StringConstant;
import com.eci.common.asset.constant.StringConstant.Symbol; 
 

/***
 * 	自定义工具类
 * 
 */
public class CustomUtil {

	private static final String[] ZN_WEEKDAYS = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" };

	private static final Pattern CAPITAL_WORD_PATTERN = Pattern.compile("[A-Z]");
	private static final Pattern LINE_PATTERN = Pattern.compile("_(\\w)");
  

	@SuppressWarnings("rawtypes")
	public static boolean isEmpty(Object obj) {
		if (obj == null) return true;
		else if (obj instanceof CharSequence)
			return ((CharSequence) obj).length() == 0;
		else if (obj instanceof Collection)
			return ((Collection) obj).isEmpty();
		else if (obj instanceof Map)
			return ((Map) obj).isEmpty();
		else if (obj.getClass().isArray())
			return Array.getLength(obj) == 0;
		return false;
	}

	public static boolean isNotEmpty(Object obj) {
		return !isEmpty(obj);
	}

	public static boolean isNull(Object obj) {
		return obj == null;
	}

	public static boolean isNotNull(Object obj) {
		return !isNull(obj);
	}

	/**
	 *	 判断某一个字符串数组中是否含有某一字符串
	 * 
	 * @param str
	 * @param strArr
	 * @return
	 */
	public static boolean isContain(String[] strArr, String str) {

		if (isEmpty(strArr) || str == null)
			return false;

		return Stream.of(strArr).anyMatch(a -> a.equals(str));
	}
	
	/**
	 *	 获取字符串数组首次出现某个字符串的位置【0开始】
	 * 	(如果未找到，则返回-1)
	 * @param str
	 * @param strArr
	 * @return
	 */
	public static int  getPosition(String[] strArr, String str) {
		if(!isContain(strArr,str))  return -1;
		
		for(int i=0;i<strArr.length;i++) {
			if(str.equals(strArr[i])){
			 return i;
			}
		 }
		return -1;
		
	}
	
	
	
	/**	驼峰转下划线  */
	public static String humpToLine(String str) {
		Matcher matcher = CAPITAL_WORD_PATTERN.matcher(str);
		StringBuffer sb = new StringBuffer();
		while (matcher.find()) {
			matcher.appendReplacement(sb,Symbol.UDLINE + matcher.group(0).toLowerCase());
		}
		matcher.appendTail(sb);
		return sb.toString();
	}
	
	/** 下划线转驼峰 */
	public static String lineToHump(String str) {
		str = str.toLowerCase();
		Matcher matcher = LINE_PATTERN.matcher(str);
		StringBuffer sb = new StringBuffer();
		while (matcher.find()) {
			matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
		}
		matcher.appendTail(sb);
		return sb.toString();
	}
		

    /**	随机产生32位的UUid(不包含'-') */  
    public static String getUUID() {
    	
        return UUID.randomUUID().toString().replace(Symbol.DASH, Symbol.BLANK);  
    }
    /*** 随机产生36位的UUid(包含'-') */  
    public static String getUUID36() {
    	
        return UUID.randomUUID().toString();  
    } 
    
    /**  生成0-1 之间随机数  包括0不包含1  */  
    public static double random() {
    	
        return Math.random();  
    }
    /**    生成0-x 之间随机数  包括0不包含x */  
    public static int random(int x) {
    	
        return new Random().nextInt(x);  
    }
 
    /**	生成一个指定长度的 包含有字母(包括大小写)和数字的随机字符串
     * 
     * */
    public static String randomAlphanumeric(int len) {
    	return RandomStringUtils.randomAlphanumeric(len);
    }
	
    /***  生成一个字节数组(当前日期)
     * return {yy,mm,dd,HH:mm:ss} 类型的byte数组
     * */
	public static byte[] generateDateByteArray(){
		String nowTime= FormatUtil.formatDateTimeForStandard(LocalDateTime.now());
		int year1=Integer.parseInt(nowTime.substring(0,2));
  		int year2=Integer.parseInt(nowTime.substring(2,4));
  		int month=Integer.parseInt(nowTime.substring(5,7));
  		int day=Integer.parseInt(nowTime.substring(8,10));
  		int hour=Integer.parseInt(nowTime.substring(11,13));
  		int min=Integer.parseInt(nowTime.substring(14,16));
  		int sec=Integer.parseInt(nowTime.substring(17)); 
  		
  		byte [] mx={(byte) year1,(byte) year2,(byte) month,(byte) day,(byte) hour,(byte) min,(byte) sec};
  		return mx;
	}
	
	/**
	  *   获取当前日期是星期几<br>
     * 
     * @param dt
     * @return 当前日期是星期几
     */
    public static String getWeekOfDate(LocalDate localDate) {
        int dayOfWeek=localDate.getDayOfWeek().getValue()-1;
        return ZN_WEEKDAYS[dayOfWeek];
    }
    
	 /**
     *	 获取当前日期是星期几<br>
     * 
     * @param dt
     * @return 当前日期是星期几
     */
    public static int getWeekDate(LocalDate localDate) { 
        
        return localDate.getDayOfWeek().getValue();
    }
    
    /**
     * 
     * 	以指定字符开头与当前时间(yyyyMMddHHmmss形式)+4位随机数组成一个新的字符串
     * 
     * */
    public static String getNewString(String start){
        Random random = new Random();
        //当前时间
        String dateStr = FormatUtil.formatDateTimeForNumber(LocalDateTime.now());
        StringBuffer code = new StringBuffer(start);
        code.append(dateStr);
        //产生4位随机数字
        for(int i=0;i<4;i++){
            code.append(String.valueOf(random.nextInt(10)));
        }
        return code.toString();
    }
	/**
	 * 	获取指定日期所属月份的最后一天
	 * */
	public static String getLastDay(LocalDate localDate){
		
		LocalDate lastDay = Optional.of(localDate).get().with(TemporalAdjusters.lastDayOfMonth());
		
		return FormatUtil.formatDateForStandard(lastDay) ;

	}
	/**
	 *	 获取指定日期所属月份的第一天
	 * */
	public static String getFirstDate(LocalDate localDate){
		
		LocalDate firstDay =Optional.of(localDate).get().with(TemporalAdjusters.firstDayOfMonth());
 
		return FormatUtil.formatDateForStandard(firstDay);
	} 
	
	/**
	 * 将提供的迭代器的元素连接到包含提供的元素的单个字符串中 
     * <pre>
     * CovertUtil.join(null, *)                = null
     * CovertUtil.join([], *)                  = ""
     * CovertUtil.join([null], *)              = ""
     * CovertUtil.join(["a", "b", "c"], "--")  = "a--b--c"
     * CovertUtil.join(["a", "b", "c"], null)  = "abc"
     * CovertUtil.join(["a", "b", "c"], "")    = "abc"
     * CovertUtil.join([null, "", "a"], ',')   = ",,a"
     * </pre>
	 * @param array
	 * @param separator
	 * @return
	 * */
    public static String join(final Iterable<?> iterable, final String separator) {
 
        return StringUtils.join(iterable.iterator(), separator);
    }
    
    /**
     *	 判断字符是否为实数
     * @param str
     * @return
     */
    public static boolean isRealNum(String str) {
        return Pattern.compile(StringConstant.Regexp.REAL_NUM).matcher(str).matches();
    }
    
    /**
     *	 判断字符是否为非负整数
     * @param str
     * @return
     */
    public static boolean isIntNum(String str) {
    	return Pattern.compile(StringConstant.Regexp.INT_NUM).matcher(str).matches();
    }
    
    /**
     *	 判断字符是否为小数
     * @param str
     * @return
     */
    public static boolean isDecimalNum(String str) {
    	return Pattern.compile(StringConstant.Regexp.DECIMAL_NUM).matcher(str).matches();
    }
    
    
}
